home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / hplip / base / slp.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2007-04-29  |  4.1 KB  |  161 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.5)
  3.  
  4. import sys
  5. import time
  6. import socket
  7. import select
  8. import struct
  9. import random
  10. import re
  11. from g import *
  12. import utils
  13. prod_pat = re.compile('\\(\\s*x-hp-prod_id\\s*=\\s*(.*?)\\s*\\)', re.IGNORECASE)
  14. mac_pat = re.compile('\\(\\s*x-hp-mac\\s*=\\s*(.*?)\\s*\\)', re.IGNORECASE)
  15. num_port_pat = re.compile('\\(\\s*x-hp-num_port\\s*=\\s*(.*?)\\s*\\)', re.IGNORECASE)
  16. ip_pat = re.compile('\\(\\s*x-hp-ip\\s*=\\s*(.*?)\\s*\\)', re.IGNORECASE)
  17. p1_pat = re.compile('\\(\\s*x-hp-p1\\s*=(?:\\d\\)|\\s*(.*?)\\s*\\))', re.IGNORECASE)
  18. p2_pat = re.compile('\\(\\s*x-hp-p2\\s*=(?:\\d\\)|\\s*(.*?)\\s*\\))', re.IGNORECASE)
  19. p3_pat = re.compile('\\(\\s*x-hp-p3\\s*=(?:\\d\\)|\\s*(.*?)\\s*\\))', re.IGNORECASE)
  20. hn_pat = re.compile('\\(\\s*x-hp-hn\\s*=\\s*(.*?)\\s*\\)', re.IGNORECASE)
  21.  
  22. def detectNetworkDevices(ttl = 4, timeout = 10):
  23.     (mcast_addr, mcast_port) = ('224.0.1.60', 427)
  24.     found_devices = { }
  25.     s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
  26.     x = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  27.     x.connect(('1.2.3.4', 56))
  28.     intf = x.getsockname()[0]
  29.     x.close()
  30.     s.setblocking(0)
  31.     ttl = struct.pack('B', ttl)
  32.     
  33.     try:
  34.         s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  35.         s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, 1)
  36.     except (AttributeError, socket.error):
  37.         pass
  38.  
  39.     
  40.     try:
  41.         s.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_TTL, ttl)
  42.         s.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_IF, socket.inet_aton(intf) + socket.inet_aton('0.0.0.0'))
  43.         s.setsockopt(socket.SOL_IP, socket.IP_MULTICAST_LOOP, 1)
  44.     except Exception:
  45.         e = None
  46.         log.error('Unable to setup multicast socket for SLP: %s' % e)
  47.         return { }
  48.  
  49.     packet = ''.join([
  50.         '\x01\x06\x00,\x00\x00en\x00\x03',
  51.         struct.pack('!H', random.randint(1, 65535)),
  52.         '\x00\x00\x00\x18service:x-hpnp-discover:\x00\x00\x00\x00'])
  53.     
  54.     try:
  55.         s.sendto(packet, 0, (mcast_addr, mcast_port))
  56.     except socket.error:
  57.         e = None
  58.         log.error('Unable to send broadcast SLP packet: %s' % e)
  59.  
  60.     time_left = timeout
  61.     while time_left > 0:
  62.         start_time = time.time()
  63.         (r, w, e) = select.select([
  64.             s], [], [
  65.             s], time_left)
  66.         time_left -= time.time() - start_time
  67.         if not r:
  68.             continue
  69.         
  70.         (data, addr) = s.recvfrom(2048)
  71.         update_spinner()
  72.         log.log_data(data, fmt = True, width = 32)
  73.         
  74.         try:
  75.             (ver, func, length, flags, dialect, lang_code, char_encode, recv_xid, status_code, attr_length) = struct.unpack('!BBHBBHHHHH', data[:16])
  76.             x = struct.unpack('!%ds' % attr_length, data[16:])[0].strip()
  77.         except struct.error:
  78.             continue
  79.  
  80.         
  81.         try:
  82.             num_ports = int(num_port_pat.search(x).group(1))
  83.         except (AttributeError, ValueError):
  84.             num_ports = 1
  85.  
  86.         if num_ports == 0:
  87.             num_ports = 1
  88.         
  89.         y = {
  90.             'num_devices': 0,
  91.             'num_ports': num_ports,
  92.             'product_id': '',
  93.             'status_code': 0,
  94.             'device2': '0',
  95.             'device3': '0',
  96.             'note': '',
  97.             'device1': '0' }
  98.         
  99.         try:
  100.             y['device1'] = p1_pat.search(x).group(1)
  101.         except AttributeError:
  102.             y['device1'] = '0'
  103.  
  104.         y['num_devices'] += 1
  105.         if num_ports > 1:
  106.             
  107.             try:
  108.                 y['device2'] = p2_pat.search(x).group(1)
  109.             except AttributeError:
  110.                 y['device2'] = '0'
  111.  
  112.             y['num_devices'] += 1
  113.             if num_ports > 2:
  114.                 
  115.                 try:
  116.                     y['device3'] = p3_pat.search(x).group(1)
  117.                 except AttributeError:
  118.                     y['device3'] = '0'
  119.  
  120.                 y['num_devices'] += 1
  121.             
  122.         
  123.         if y['device1'] is None:
  124.             y['device1'] = '0'
  125.         
  126.         if y['device2'] is None:
  127.             y['device2'] = '0'
  128.         
  129.         if y['device3'] is None:
  130.             y['device3'] = '0'
  131.         
  132.         
  133.         try:
  134.             y['product_id'] = prod_pat.search(x).group(1)
  135.         except AttributeError:
  136.             y['product_id'] = ''
  137.  
  138.         
  139.         try:
  140.             y['mac'] = mac_pat.search(x).group(1)
  141.         except AttributeError:
  142.             y['mac'] = ''
  143.  
  144.         
  145.         try:
  146.             y['ip'] = ip_pat.search(x).group(1)
  147.         except AttributeError:
  148.             y['ip'] = ''
  149.  
  150.         
  151.         try:
  152.             y['hn'] = hn_pat.search(x).group(1)
  153.         except AttributeError:
  154.             y['hn'] = ''
  155.  
  156.         y['status_code'] = status_code
  157.         found_devices[addr[0]] = y
  158.         log.debug('Found device: %s' % y)
  159.     return found_devices
  160.  
  161.